package com.yanqu.road.dao.impl.union.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.trade.CrossUnionTradeRouteUnLockData;
import com.yanqu.road.entity.union.trade.UnionTradeHiddenRouteExpiry;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionTradeRouteUnLockDataDaoImpl extends TempDataObjectDao<CrossUnionTradeRouteUnLockData> {


    public static String getCrossUnionTradeRouteUnLockDataSql = "select * from `t_c_union_trade_route_unlock` ";
    public Map<String, CrossUnionTradeRouteUnLockData> getCrossUnionTradeRouteUnLockData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, CrossUnionTradeRouteUnLockData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossUnionTradeRouteUnLockDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionTradeRouteUnLockData data = parse(rs);
                result.put(data.getUnionUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertCrossUnionTradeRouteUnLockDataSql = "insert into `t_c_union_trade_route_unlock`(unionUid,routeExpiryList) values (?,?)";

    @Override
    protected boolean addData(CrossUnionTradeRouteUnLockData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRouteExpiryList())));
        if (getDb().executeNoneQuery(insertCrossUnionTradeRouteUnLockDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossUnionTradeRouteUnLockDataSql = "update `t_c_union_trade_route_unlock` set routeExpiryList=? where unionUid=?";

    @Override
    protected boolean updateData(CrossUnionTradeRouteUnLockData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRouteExpiryList())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        if (getDb().executeNoneQuery(updateCrossUnionTradeRouteUnLockDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossUnionTradeRouteUnLockData parseData(ResultSet rs) throws SQLException {
        CrossUnionTradeRouteUnLockData data = new CrossUnionTradeRouteUnLockData();
        data.setUnionUid(rs.getString("unionUid"));
        data.setRouteExpiryList(JSON.parseObject(rs.getString("routeExpiryList"), new TypeReference<List<UnionTradeHiddenRouteExpiry>>() {}));

        return data;
    }
}
